package com.tutor.syslib.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.tutor.syslib.Logs;

import java.io.File;

/**
 * Created by zzgpc on 2017-9-10.
 * 6045
 *
 * 系统管理类
 */
public class AndroidUtils {

    /**
     * 获取Android版本对应的sdk int值
     **/
    public static int getAndroidVersion() {

        // Log.e( tag, "手机版本的sdk值" + android.os.Build.VERSION.SDK_INT );
        return android.os.Build.VERSION.SDK_INT;
    }


    /**
     * 查看是否有某个权限
     *
     * @param context
     * @return
     */
    public static boolean getPermissionStatus(Context context, String permission) {
        String packName = context.getPackageName();
        PackageManager pm = context.getPackageManager();
        boolean flag = (PackageManager.PERMISSION_GRANTED ==
                pm.checkPermission(permission, packName));
        if (flag) {
            //有这个权限，做相应处理
            return true;
        } else { //没有权限
            return false;
        }
    }


    /**
     * 以下代码可以跳转到应用详情，可以通过应用详情跳转到权限界面(6.0系统测试可用)
     *
     * @param context
     */
    public static void getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        context.startActivity(localIntent);
    }

    public static void closeKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    /**
     * 获取当前客户端版本信息 void
     *
     * @exception
     * @since 1.0.0
     */
    public static int getCurrentVersion( Context context ) {

        try {
            PackageInfo info = context.getPackageManager().getPackageInfo( context.getPackageName(), 0 );
            int curVersionCode = info.versionCode;
            return curVersionCode;
        } catch( PackageManager.NameNotFoundException e ) {
            e.printStackTrace( System.err );
            return -1;
        }
    }

    /**
     * 获取当前客户端版本信息 void
     *
     * @exception
     * @since 1.0.0
     */
    public static String getCurrentVersionName( Context context ) {

        try {
            PackageInfo info = context.getPackageManager().getPackageInfo( context.getPackageName(), 0 );
            String curVersionName = info.versionName;
            return curVersionName;
        } catch( PackageManager.NameNotFoundException e ) {
            e.printStackTrace( System.err );
            return null;
        }
    }

    /**
     * 打开apk文件
     *
     * @param context
     * @param file
     *            void
     * @exception
     * @since 1.0.0
     */
    public static void inStallApk( Context context, File file ) {

        if( !file.exists() ) {
            return;
        }
        Logs.e( "AndroidUtils", file.getName() );
        Intent intent = new Intent();
        intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
        intent.setAction( android.content.Intent.ACTION_VIEW );
        intent.setDataAndType( Uri.fromFile( file ), "application/vnd.android.package-archive" );
        context.startActivity( intent );
    }
}
